package random.fox.p2p.api;

import android.util.Log;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonArrayRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import random.fox.p2p.AppConfig;

/**
 * 网络工具类,真正调用是Volley
 * Created by wq on 15/3/16.
 */
public class ApiClient {
    private String TAG = "ApiClient";
    private RequestQueue mRequestQueue;

    public ApiClient(RequestQueue requestQueue) {
        this.mRequestQueue = requestQueue;
    }

    /**
     * 登陆接口
     *
     * @param username
     * @param password
     * @param callBack
     */
    public void login(String username, String password, CallBack callBack) throws Exception {
        JSONObject object = new JSONObject();
        object.put("username", username);
        object.put("password", password);
        Request loginRequest = new JsonObjectRequest(APIs.loginApi, callBack, object.toString());
        request(loginRequest);
    }

    /**
     * 获取验证码接口
     *
     * @param phoneNumber
     * @param callBack
     */
    public void getCode(String phoneNumber, CallBack callBack) throws Exception {
        JSONObject object = new JSONObject();
        object.put("pnumber", phoneNumber);
        Request request = new JsonObjectRequest(APIs.getCode, callBack, object.toString());
        request(request);
    }

    /**
     * 设置密码接口
     *
     * @param phoneNumber
     * @param password
     * @param callBack
     * @throws Exception
     */
    public void setPassword(String phoneNumber, String password, CallBack callBack) throws Exception {
        JSONObject object = new JSONObject();
        object.put("pnumber", phoneNumber);
        object.put("password", password);
        Request request = new JsonObjectRequest(APIs.setPassword, callBack, object.toString());
        request(request);
    }

    /**
     * 获取用户信息 包含动态 个性标签 粉丝
     *
     * @param callBack
     */

    public void getUserInfo(CallBack callBack) {
        Request request = new JsonObjectRequest(APIs.getUserInfo, callBack, "");
        request(request);
    }

    public void test(CallBack callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("uploadInfo", "1");
        params.put("test1", "2");
        File imageFile = new File("/sdcard/a.jpg");
        params.put("filename", imageFile.getName());
        PostImageRequest request = new PostImageRequest(APIs.uploadInfo, callBack, params, imageFile);
        request(request);
    }

    public void setInfo(Map<String, String> params, File file, CallBack callBack) {
        Request request = new PostImageRequest(APIs.uploadInfo, callBack, params, file);
        request(request);
    }

    public void sendMessage(Map<String, String> params, File file, CallBack callBack) {
        Request request = new PostImageRequest(APIs.sendMessage, callBack, params, file);
        request(request);
    }

    public void addLike(String uid, int mid, CallBack callBack) throws JSONException {
        JSONObject object = new JSONObject();
        object.put("uid", uid);
        object.put("mid", mid);
        Request request = new JsonObjectRequest(APIs.likeMessage, callBack, object.toString());
        request(request);
    }

    public void addReply(String uid, int mid, String content, CallBack callBack) throws JSONException {
        JSONObject object = new JSONObject();
        object.put("uid", uid);
        object.put("mid", mid);
        object.put("content", content);
        Request request = new JsonObjectRequest(APIs.addReply, callBack, object.toString());
        request(request);
    }

    public void todayHot(String uid, CallBack callBack) throws JSONException {
        JSONObject object = new JSONObject();
        object.put("uid", uid);
        Request request = new JsonArrayRequest(Request.Method.POST, APIs.todayHot, object.toString(), callBack, callBack);
        request(request);
    }

    public void getLikeList(String uid, CallBack callBack) throws JSONException {
        JSONObject object = new JSONObject();
        object.put("uid", uid);
        Request request = new JsonArrayRequest(Request.Method.POST, APIs.getLikeList, object.toString(), callBack, callBack);
        request(request);
    }

    private <T> void request(Request request) {
        Log.d(TAG, "network:URL:\t" + request.getUrl());
        mRequestQueue.add(request);
        mRequestQueue.start();
    }

    /**
     * 请求时，需要实现回调
     *
     * @param <T>
     */
    public static abstract class CallBack<T> implements Response.Listener<T>, Response.ErrorListener {

        @Override
        public abstract void onResponse(T t);
    }

    /**
     * 定义api url
     */
    private static final class APIs {
        private APIs() {
        }

        static String DEBUG_HOST = "http://123.57.78.179:3000";

        static String RELEASE_HOST = "";
        static String HOST = AppConfig.DEBUG ? DEBUG_HOST : RELEASE_HOST;
        static String loginApi = HOST + "/login";

        static String getCode = HOST + "/getCode";
        static String registerApi = HOST + "/register";
        static String setPassword = HOST + "/setPassword";
        static String uploadInfo = HOST + "/uploadInfo";

        //今日发现
        static String todayHot = HOST + "/todayHot";
        static String sendMessage = HOST + "/message/new";

        static String likeMessage = HOST + "/likeMessage";
        static String temp = HOST + "/api";
        static String getUserInfo = temp + "/getUserInfo";

        static String getLikeList = temp + "/getLikeList";

        static String addReply = temp + "/addReply";
    }

}
